package com.consume.bhy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.consume.bhy.service.PhotoService;

@WebServlet(urlPatterns="/consume/photoServlet")
public class PhotoServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Class<?> class1 = this.getClass();	
		String methodName = request.getParameter("method");
		
		try {
			Method method = class1.getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			method.setAccessible(true);
			try {
				method.invoke(this, request,response);
				
			} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
				e.printStackTrace();
			}
			
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		}
		
	}

	public void photo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/palin;charset=utf-8");

		//创建一个JSON数组存放返回的数据
		JSONArray jsonData = new JSONArray();
		
        PhotoService ps = new PhotoService();
        jsonData = ps.getJsonData();

		//获取输出流写数据
        PrintWriter writer = response.getWriter();
        writer.print(jsonData);
		
	}
	
}
